package com.punai.datapush.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.datapush.domain.EcoFeeitemGuideprice;
import com.punai.datapush.domain.EcoFeeitemPeriod;
import com.punai.datapush.domain.vo.FeeitemPeriodVo;
import com.punai.datapush.mapper.EcoFeeitemGuidepriceMapper;
import com.punai.datapush.mapper.EcoFeeitemPeriodMapper;
import com.punai.datapush.service.IEcoFeeitemPeriodService;
import com.punai.evaluationSystem.costDataEvalute.domain.EvaluationDataPrice;
import com.punai.evaluationSystem.costDataEvalute.mapper.EvaluationDataPriceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 期次维护Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-19
 */
@Service
public class EcoFeeitemPeriodServiceImpl extends ServiceImpl<EcoFeeitemPeriodMapper, EcoFeeitemPeriod> implements IEcoFeeitemPeriodService {
    @Autowired
    private EcoFeeitemPeriodMapper ecoFeeitemPeriodMapper;
    @Autowired
    private EcoFeeitemGuidepriceMapper feeitemGuidepriceMapper;
    @Autowired
    private EvaluationDataPriceMapper evaluationDataPriceMapper;


    /**
     * 查询期次维护
     *
     * @param id 期次维护主键
     * @return 期次维护
     */
    @Override
    public EcoFeeitemPeriod selectEcoFeeitemPeriodById(String id) {
        return ecoFeeitemPeriodMapper.selectEcoFeeitemPeriodById(id);
    }

    /**
     * 查询期次维护列表
     *
     * @param ecoFeeitemPeriod 期次维护
     * @return 期次维护
     */
    @Override
    public List<EcoFeeitemPeriod> selectEcoFeeitemPeriodList(EcoFeeitemPeriod ecoFeeitemPeriod) {
        return ecoFeeitemPeriodMapper.selectEcoFeeitemPeriodList(ecoFeeitemPeriod);
    }

    /**
     * 新增期次维护
     *
     * @param vo 期次维护
     * @return 结果
     */
    @Override
    @Transactional
    public int insertEcoFeeitemPeriod(FeeitemPeriodVo vo) {
        //
        String name = vo.getName();
        Long aLong = ecoFeeitemPeriodMapper.selectCount(Wrappers.<EcoFeeitemPeriod>lambdaQuery().eq(EcoFeeitemPeriod::getName, name));
        if (aLong > 0) {
            throw new RuntimeException("期次名称已存在");
        }

        EcoFeeitemPeriod ecoFeeitemPeriod = new EcoFeeitemPeriod();
        ecoFeeitemPeriod.setName(name);
        ecoFeeitemPeriod.setStart(vo.getStart());
        ecoFeeitemPeriod.setEnd(vo.getEnd());
        ecoFeeitemPeriod.setEvaluationIds(vo.getEvaluationIds());
        int insert = ecoFeeitemPeriodMapper.insert(ecoFeeitemPeriod);

        if (StringUtils.isNotBlank(vo.getEvaluationIds())) {
            String[] ids = vo.getEvaluationIds().split(",");
            List<EvaluationDataPrice> list = evaluationDataPriceMapper.selectPJpriceData(ids);
            if (CollUtil.isNotEmpty(list)) {
                List<EcoFeeitemGuideprice> guideList = list.stream().map(item -> {
                    EcoFeeitemGuideprice guideprice = new EcoFeeitemGuideprice();
                    guideprice.setCode(item.getCode());
                    guideprice.setName(item.getName());
                    guideprice.setPeriodId(ecoFeeitemPeriod.getId());
                    guideprice.setSpecName(item.getTzz());
                    guideprice.setUnit(item.getJldw());
                    guideprice.setNotaxprice(item.getUserPrice());
                    guideprice.setCreateType("0");
                    guideprice.setDatafrom("EvaluationSystem");
                    return guideprice;
                }).collect(Collectors.toList());
                feeitemGuidepriceMapper.insertBatchSomeColumn(guideList);
            }
        }

        String wqprice = vo.getWqprice();
        if (StrUtil.isNotEmpty(wqprice)) {
            feeitemGuidepriceMapper.copyData(wqprice, ecoFeeitemPeriod.getId());
        }
        return insert;
    }

    /**
     * 修改期次维护
     *
     * @param ecoFeeitemPeriod 期次维护
     * @return 结果
     */
    @Override
    public int updateEcoFeeitemPeriod(EcoFeeitemPeriod ecoFeeitemPeriod) {
        return ecoFeeitemPeriodMapper.updateById(ecoFeeitemPeriod);
    }

    /**
     * 批量删除期次维护
     *
     * @param ids 需要删除的期次维护主键
     * @return 结果
     */
    @Override

    public int deleteEcoFeeitemPeriodByIds(String[] ids) {
        int i = ecoFeeitemPeriodMapper.deleteEcoFeeitemPeriodByIds(ids);

        return i;
    }

    /**
     * 删除期次维护信息
     *
     * @param id 期次维护主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteEcoFeeitemPeriodById(String id) {
        int i = ecoFeeitemPeriodMapper.deleteEcoFeeitemPeriodById(id);
        if (i > 0) {
            feeitemGuidepriceMapper.delete(Wrappers.<EcoFeeitemGuideprice>lambdaQuery().eq(EcoFeeitemGuideprice::getPeriodId, id));
        }
        return i;
    }
}
